package com.example.imagepicker.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;

import com.example.imagepicker.activity.PhotoScanActivity;
import com.example.imagepicker.activity.PhotoWallActivity;
import com.example.imagepicker.model.Photo;
import com.example.imagepicker.utils.SDCardImageLoader;
import com.example.imagepicker.utils.ScreenUtils;
import com.example.imagepicker.utils.Utility;
import com.tool.R;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * PhotoWall中GridView的适配器
 *
 * @author hanj
 */

public class PhotoWallAdapter extends BaseAdapter {
    private Context context;
    private List<Photo> imagePathList = new ArrayList<Photo>();
    private List<String> data = new ArrayList<String>();
    
    private SDCardImageLoader loader;
    
    private Button confirmBtn;
    private int needCount;

    public PhotoWallAdapter(Context context, List<Photo> imagePathList,List<String> data,Button confirmBtn,int needCount) {
        this.context = context;
        this.imagePathList = imagePathList;
        this.data = data;
        this.confirmBtn = confirmBtn;
        this.needCount = needCount;
        
        loader = new SDCardImageLoader(ScreenUtils.getScreenW(), ScreenUtils.getScreenH());
    }

    @Override
    public int getCount() {
        return imagePathList == null ? 0 : imagePathList.size();
    }

    @Override
    public Object getItem(int position) {
        return imagePathList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.photo_wall_item, null);
            holder = new ViewHolder();

            holder.imageView = (ImageView) convertView.findViewById(R.id.photo_wall_item_photo);
            holder.checkBox = (ImageView) convertView.findViewById(R.id.photo_wall_item_cb);
            holder.photo_item_click = (ImageView) convertView.findViewById(R.id.photo_item_click);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Photo vo = imagePathList.get(position);
        
        if(vo.getStatus()==1){
            holder.checkBox.setImageDrawable(context.getResources().getDrawable(R.drawable.checkbox_checked));
            holder.imageView.setColorFilter(context.getResources().getColor(R.color.image_checked_bg));
        }else{
            holder.checkBox.setImageDrawable(context.getResources().getDrawable(R.drawable.checkbox_normal));
            holder.imageView.setColorFilter(null);
        }
        
        holder.imageView.setTag(vo.getImgPath());
        loader.loadImage(4, vo.getImgPath(), holder.imageView);
        
        holder.checkBox.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if(vo.getStatus()==0){
                    if(needCount>0&&data.size()>=needCount){
                        Utility.showToast(context, "选择图片不能超过"+needCount+"张");
                    }else{
                        vo.setStatus(1);
                        if(!data.contains(vo.getImgPath())){
                            data.add(vo.getImgPath());
                        }else{
                            Log.d("", vo.getImgPath()+"图片位置添加出错");
                        }
                    }
                    
                }else{
                    vo.setStatus(0);
                    if(data.contains(vo.getImgPath())){
                        data.remove(vo.getImgPath());
                    }else{
                        Log.d("", vo.getImgPath()+"图片位置去除出错");
                    }
                    
                }
                if(needCount>0){
                    confirmBtn.setText("("+data.size()+"/"+needCount+")完成");
                }else{
                    confirmBtn.setText("("+data.size()+")完成"); 
                }
                notifyDataSetChanged();
               
            }
        });
        
        holder.photo_item_click.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PhotoScanActivity.class);
                intent.putExtra("paths", (Serializable)imagePathList);
                intent.putExtra("position", position);
                intent.putExtra("NeedCount", needCount);
                ((PhotoWallActivity)context).startActivityForResult(intent, PhotoWallActivity.requestCode_scan);
            }
        });
        return convertView;
    }

    private class ViewHolder {
        ImageView imageView;
        ImageView checkBox;
        ImageView photo_item_click;
    }
    
    public List<String> getData() {
        return data;
    }
    
}
